package thread;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Creared with IntelliJ IDEA.
 * Description:原子类 CAS
 * User:yxd
 * Date:2022-03-31
 * Time:0:07
 */
public class Demo27 {
    public static void main(String[] args) throws InterruptedException {
        AtomicInteger count = new AtomicInteger(0);//原子类,初始化一个0
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 500; i++) {
                count.getAndIncrement();//这个操作就相当于num++

            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 500; i++) {
                count.getAndIncrement();

            }
        });
//        count.incrementAndGet();//++count的功能
//        count.decrementAndGet();//--num
//        count.getAndDecrement();//num--
//        count.getAndAdd(10);//+=10
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(count.get());//通过get方法获得到原子类内部的值
    }
}
